/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/** 
 * ClassName: DBHelper <br/> 
 * Function: TODO ������ <br/> 
 * date: 2017��11��1�� ����10:08:47 <br/> 
 * 
 * @author xuaner 
 * @version  
 * @since JDK 1.8 
 */
public class DBHelper {
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	private static final String PATH="/cfg.properties";
	
	//���������ļ�
	static {
		InputStream fis=null;
		try {
			
			fis=DBHelper.class.getResourceAsStream(PATH);
			Properties pro=new Properties();
			pro.load(fis);
			driver=pro.getProperty("jdbc.driver");
			url=pro.getProperty("jdbc.url");
			user=pro.getProperty("jdbc.user");
			password=pro.getProperty("jdbc.password");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.println("�����ļ������ڣ�");
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("���������ļ�ʧ�ܣ�");
		}finally {
			try {
				if(fis!=null)fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		 }
		}
	//��������
     static {
    	 try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.err.println("��������ʱ���ִ���");
		}
	}
     /** 
	 * openConnection:(������). <br/>
	 * 
	 * @author xuaner  
	 * @since JDK 1.8 
	 */
	private void openConnection()throws SQLException,ClassNotFoundException {
		if(conn==null||conn.isClosed()) {
			conn=DriverManager.getConnection(url, user, password);
		}
	}
	/** 
	 * close:(�ر�����). <br/>
	 * 
	 * @author xuaner  
	 * @since JDK 1.8 
	 */
	public void close()throws SQLException {
		 if(conn!=null)conn.close();
    	 if(ps!=null)ps.close();
    	 if(rs!=null)rs.close();
	}
	
	
	/** 
	 * query:(ȫ��ķ���). <br/>
	 * 
	 * @author xuaner  
	 * @since JDK 1.8 
	 */
	 public ResultSet queryAll(String sql,Object ...values )throws SQLException,ClassNotFoundException {
	    	openConnection();
	    	ps=conn.prepareStatement(sql);
	    	setParameter(values);
	    	rs=ps.executeQuery();
	    	return rs;
		}

	public int executeUpdate(String sql,Object ...values)throws SQLException , ClassNotFoundException {
		openConnection();
		ps=conn.prepareStatement(sql);
		setParameter(values);
		return ps.executeUpdate();
	}
	/** 
	 * setParameter:(��ռλ����ֵ). <br/>
	 * 
	 * @author xuaner 
	 * @param values 
	 * @since JDK 1.8 
	 */  
	private void setParameter(Object ...values)throws SQLException,ClassNotFoundException {
		if(values!=null && values.length>0) {
			for(int i=0;i<values.length;i++) {
				ps.setObject(i+1, values[i]);
			}
		}
	}
	
	
	

}
